//package com.gba.mark.origin.service.impl;
//
///*
//测试未成功，寻找原因中
// */
//
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.gba.mark.origin.entity.Origin;
//import com.gba.mark.origin.mapper.OriginMapper;
//import com.gba.mark.origin.mapper.OriginRepository;
//import com.gba.mark.origin.service.IESOriginService;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.unit.TimeValue;
//import org.elasticsearch.index.query.MatchQueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//
//@Service
//    public class ESOriginServiceImpl extends ServiceImpl<OriginMapper, Origin> implements IESOriginService {
//
//
//        @Autowired
//        private OriginRepository originRepository;
//
//        @Resource
//        private OriginMapper originMapper;
//
//        @Resource
//        private RestHighLevelClient client;
//
//        @Resource
//        private IESOriginService iesOriginService;
//
//    /*
//        本地测试未通过
//     */
//    @Override
//
//    //限定来源查询字和词
//
//    public List<Map<String, Object>> findByWord(String word,int pageNo,int pageSize,String source) throws IOException {
//
//
//        //指定要查询的索引
//        SearchRequest searchRequest = new SearchRequest("gba_corpus");
//
//        // 设置分页条件
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        pageNo = pageNo == 0 ? 1 : pageNo;
//        pageSize = pageSize == 0 ? 10 : pageSize;
//        searchSourceBuilder.from(pageNo);
//        searchSourceBuilder.size(pageSize);
//
//        // 输入的关键字匹配的字段
//
//        MatchQueryBuilder termQueryBuilder = QueryBuilders.matchQuery("sentence",word);
//        searchSourceBuilder.query(termQueryBuilder);
//        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
//
//        //执行
//        searchRequest.source(searchSourceBuilder);
//
//        SearchResponse search = client.search(
//                searchRequest,
//                RequestOptions.DEFAULT);
//
//        ArrayList<Map<String,Object>> list = new ArrayList<>();
//
//        //关闭es客户端
//        client.close();
//
//        //输出数据条数和数据列表
//        if (search.getHits().getHits().length!=0){
//            for (SearchHit documentFields : search.getHits().getHits()) {
//                list.add(documentFields.getSourceAsMap());
//            }
//            System.out.println(search.getHits().getTotalHits());
//            return list;
//        } else {
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("code",404);
//            map.put("msg","没有相关数据");
//            list.add(map);
//            return list;
//        }
//    }
//}

//        此处是另一个本地测试没通过的方法
//        public Map<String, Object> findByWord(String word) throws Exception {
//            //创建ES客户端
//            RestHighLevelClient esClient = new RestHighLevelClient(
//                    RestClient.builder(new HttpHost("localhost",9200,"http"))
//            );
//
//            //查询用户输入的字词的对应数据
//            SearchRequest request = new SearchRequest();
//            request.indices("origin");
//            SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchQuery("sentence", word));
//
//            //分页查询
//            builder.from(0);
//            builder.size(10);
//
//            request.source(builder);
//
//            SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
//            SearchHits hits = response.getHits();
//
//            System.out.println(hits.getTotalHits());
//            System.out.println(response.getTook());
//
//            for ( SearchHit hit : hits){
//                System.out.println(hit.getSourceAsString());
//            }
//
//
//            //关闭ES客户端
//            esClient.close();
//
//            //输出标注结果
//            Page<Origin> searchResponse = originRepository.search(hits);
//            List<Origin> origins = searchResponse.getContent();
//            List<Integer> originIds = new ArrayList<>();
//            for (Origin origin1 : origins) {
//                originIds.add(origin1.getId());
//            }
//            Map<String, Object> map = new HashMap<>();
//            map.put("originIds", originIds);
//            map.put("total", new Long(searchResponse.getTotalElements()).intValue());
//            return map;
//  }
